/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo.thread;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author VANDH
 */
public class TinhFibo extends Thread {

    public int fiboThuN = 0;
    public int n = 0;
    public AtomicInteger sc;

    @Override
    public void run() {
        if (n <= 1) {
            fiboThuN = n;
        }

        int f1 = 0, f2 = 1, f = 1;
        for (int i = 2; i <= n; i++) {
            // tính phần tử thứ i
            f = f1 + f2;
            f1 = f2;
            f2 = f;

            // test cai coi
            try {
                Thread.sleep(1000);
                synchronized (sc) {
                    sc.addAndGet(1);
                    System.out.println("Fibo - " + sc);
                    sc.notify();
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(JPII1208A.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        fiboThuN = f;
        System.out.println("Fibo thứ " + this.n + " = " + fiboThuN);
    }
}
